package com.wiken.orderservice.service.impl;

import com.wiken.orderservice.entity.OrderCommodity;
import com.wiken.orderservice.mapper.OrderCommodityMapper;
import com.wiken.orderservice.service.IOrderCommodityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wiken.servicecommon.common.Code;
import com.wiken.servicecommon.common.exception.DefaultException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wiken
 * @since 2022-12-28
 */
@Service
public class OrderCommodityServiceImpl extends ServiceImpl<OrderCommodityMapper, OrderCommodity> implements IOrderCommodityService {

    @Override
    public boolean addOrderCommodity(OrderCommodity orderCommodity) {
        int insert = baseMapper.insert(orderCommodity);

        return insert > 0;
    }

    @Override
    public List<OrderCommodity> getOrderCommodityListByCommodityList(List<OrderCommodity> orderCommodities) {
        List<String> ids = new ArrayList<>();

        for(OrderCommodity orderCommodity : orderCommodities){
            if(orderCommodity.getCommodityId() == null) throw new DefaultException(Code.ERROR_ORDER_COMMODITY_ID_NULL, "订单异常");
            ids.add(orderCommodity.getCommodityId());
        }
        List<OrderCommodity> orderCommodityList = baseMapper.getOrderCommodityListByCommodityIds(ids);
        return orderCommodityList;
    }

    @Override
    public boolean updateOrderCommodityStatus(String orderCommodityId, Integer status) {
        OrderCommodity orderCommodity = baseMapper.selectById(orderCommodityId);

        orderCommodity.setStatus(status);

        int success = baseMapper.updateById(orderCommodity);

        return success > 0;
    }
}
